
default:
	@echo "Try: "
	@echo "  make package"
	@echo "  make test"
	@exit 1

T=testdir
V=versabox.tmp
P=${PWD}/$V/bin
VP:=PATH=${PWD}/$V/bin LD_LIBRARY_PATH=${PWD}/$V/lib
VPSH=${VP} $P/sh
VRUN:=${VPSH} $P/versatar
NONEMPTY:=grep -q .

CXXFLAGS=-I/usr/include/xplc-0.3.13 -I/usr/include/xplc-0.3.11 -I/usr/include/wvstreams

wv: LDFLAGS=-lwvstreams -s

unicopy: LDFLAGS=-luniconf -s

wv: wv.o

unicopy: unicopy.o

test1:
	rm -rf $T $T.? test?.tar test.lst.tmp test.zip
	mkdir $T
	touch $T/a $T/b $T/c
	${VRUN} -cf test1.tar -w test1.stamp $T
	${NONEMPTY} test1.tar
	${VRUN} -cF test.zip $T
	cp -a $T $T.1
	${VRUN} -cf test2.tar -s test1.stamp $T
	${NONEMPTY} test2.tar
	${VRUN} -cF test.zip -s test1.stamp $T
	cp -a $T $T.2
	sleep 1
	echo content >$T/b
	${VRUN} -cf test3.tar -s test1.stamp $T
	${NONEMPTY} test3.tar
	${VRUN} -cF test.zip -s test1.stamp $T
	cp -a $T $T.3
	sleep 1
	echo content2 >$T/d
	${VRUN} -cf test4.tar -s test1.stamp -w test4.stamp $T
	${NONEMPTY} test4.tar
	${VRUN} -cF test.zip -s test1.stamp $T
	cp -a $T $T.4
	sleep 1
	echo contest3 >$T/d
	${VRUN} -cf test5.tar -s test4.stamp $T
	${NONEMPTY} test5.tar
	${VRUN} -cF test.zip -s test4.stamp $T
	cp -a $T $T.5

test2:
	set -e; for d in 1 2 3 4 5; do \
		echo "Checking table of contents in test$$d.tar."; \
		${VRUN} -tf test$$d.tar >test.lst.tmp; \
		diff -q test$$d.lst test.lst.tmp; \
		rm -f test.lst.tmp; \
		echo "  ok."; \
	done

test3:
	rm -rf $T
	set -e; for d in 1 2 3 4 5; do \
		echo "Checking results after extracting test$$d.tar:"; \
		${VRUN} -xvf test$$d.tar; \
		diff -ur $T.$$d/ $T/; \
		echo "  ok."; \
	done
	
test4:
	rm -rf $T
	${VRUN} -xvF test.zip
	diff -ur $T.5 $T
	@echo "  ok."
	
testp1:
	rm -rf fs.tmp test.vip
	mkdir fs.tmp fs.tmp/filesystem
	ln testproc.sh fs.tmp/filesystem/proc.sh
	cd fs.tmp && ${VRUN} -cF ../test.vip filesystem/
	rm -f vip.ini
	cp testvip.ini vip.ini
	zip -m test.vip vip.ini
	${VPSH} versapack test.vip VIP-Test.tmp
	
test-nvs:
	${VPSH} versapack nvs.vip NVS-Test.tmp
	

test: package test1 test2 test3 test4 testp1



package-stage1: wv unicopy
	# Basic set of directories and binaries
	rm -rf $V $V.bak
	mkdir -p $V/bin $V/lib $V/tmp $V/etc
	ln -s . $V/filesystem
	touch $V/etc/mtab
	ln -v etcprofile $V/etc/profile
	ln -v versa-pre.ini versa-post.ini \
		versa-proc.sh proc.sh subproc.sh $V/
	ln -v versatar versapack versa-repack-self vipinstall loginsh \
		wv wvkick unicopy fullname \
		/bin/busybox $V/bin/
	PATH=$$PATH:/sbin:/usr/sbin cp -v `which ash basename dirname date awk find pax gzip zip unzip mkisofs umount uni uniconfd` $V/bin/
	ln -sv ash $V/bin/sh
	
	# Create symlinks for busybox commands
	set -e; $V/bin/busybox 2>&1 | perl -e \
		'$$a = join("", <>); $$a =~ s/.*defined functions://s; $$a =~ s/\s+//gs; print join("\n", split(",", $$a))' \
	    | while read x; do \
	    	[ ! -e $V/bin/$$x ] && ln -sv busybox $V/bin/$$x; \
	      done
	
	# Copy in libraries we depend on
	cp -v /lib/ld-linux.so.2 $V/lib/
	cp -v $$(ldd $$(find $V/bin -type f) \
		| perl -ne 'if (m{=> (/[^ ]*)}) { \
			$$s=$$1; $$s =~ s{/tls|/i.86|/cmov}{}g; \
			print "$$s\n" \
		   }' \
		| sort \
		| uniq) $V/lib/

package-stage2:
	# Generate the tarball, then extract it again to ensure it's
	# enough for the next phase
	cd $V && ${VP} versa-repack-self
	${NONEMPTY} $V/versabox.tar.gz
	mv $V/versabox.tar.gz .
	mv $V $V.bak
	mkdir $V
	cd $V && tar -xzf ${PWD}/versabox.tar.gz
	
package: package-stage1 package-stage2

clean:
	rm -rf test?.stamp test?.tar test.zip $T $T.? *.bak *~ *.tmp
	rm -f versabox.tar.gz wv unicopy test.vip *.o
